<?php
namespace App\Http\Controllers\Api\Mall;

use App\Http\Controllers\Controller;
use App\Models\Mall\Goods;
use App\Models\Mall\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class OrderController extends Controller 
{
	//用户订单列表
	public function index( Request $request )
	{
		$user = auth()->user();

		$orders = Order::where('user_id', $user->id)->orderBy('id', 'desc')->where('status', 1)->paginate(10);
		foreach ($orders as $order) {
			foreach ($order->items as $item) $item->goods;
		}

		return response()->json(['code' => 1000, 'data' => $orders])->withCallback($request->input('callback'));
	}

	public function detail( Request $request )
	{
		$user = auth()->user();
		$order = Order::where('user_id', $user->id)->where('id', $request->input('id'))->first();
		foreach ($order->items as $item) $item->goods;
		
		return response()->json(['code' => 1000, 'data' => $order])->withCallback($request->input('callback'));
	}

	//更新订单数据
	public function update( Request $request )
	{
		$user = auth()->user();

		$id = $request->input('id');
		$order = Order::findOrFail($id);
		if ( $order->user_id != $user->id ) {
			return response()->json(['code' => 1000, 'msg' => 'order error' ]);
		}

		$data = [];
        $fields = ['realname', 'phone', 'address', 'remark'];
        foreach ($fields as $field) {
            if ($request->has($field)) {
                $order->$field = $request->input($field);
            }
        }

        try {
            DB::beginTransaction();

            $order->save();

            DB::commit();
        } catch (\Exception $e) {
            DB::rollback();
            return response()->json(['code' => 9000, 'msg' => $e->getMessage()]);
        }

        return response()->json(['code' => 1000, 'msg' => 'ok']);
	}

	//快递查询
	public function express( Request $request )
	{
		$user = auth()->user();
		$order = Order::findOrFail( $request->input('id') );
		if ( $order->user_id != $user->id ) {
			response()->json(['code' => 1001, 'msg' => 'user error'])->withCallback($request->input('callback'));
		}

		if ( empty($order->express_code) ) {
			response()->json(['code' => 1002, 'msg' => '尚未发货']);
		}

		$key = 'mall_order_express_order_id_' . $order->id;
		$res = Redis::get( $key );
		if ( !$res ) {
			
		    $method = "GET";
		    $appcode = "1e09a7537ac1425b91a1e784287635e0";
		    $headers = array();
		    array_push($headers, "Authorization:APPCODE " . $appcode);
		    $querys = "number=". $order->express_code ."&type=auto";
		    $bodys = "";
		    $url = 'http://jisukdcx.market.alicloudapi.com/express/query?' . $querys;

		    $curl = curl_init();
		    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		    curl_setopt($curl, CURLOPT_URL, $url);
		    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		    curl_setopt($curl, CURLOPT_FAILONERROR, false);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		    curl_setopt($curl, CURLOPT_HEADER, FALSE);

		    $res = curl_exec($curl);

		    Redis::setex( $key, 3600, $res );
		}

		return response()->json(['code' => 1000, 'data' => json_decode($res)])->withCallback($request->input('callback'));
	}
}